
package com.powersi.biz.util.report.xls.impl;

import java.lang.reflect.Method;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.powersi.biz.util.report.common.ResConfig;
import com.powersi.biz.util.report.object.Pojo;
import com.powersi.biz.util.report.xls.ExcelFormat;

/**
 * excel报表
 */
public class ExcelReport{
	/**
	 * 生成基础excel模型
	 * @param request
	 * @param response
	 * @param title				报表标题
	 * @param prefix			资源配置中常量前缀
	 * @param columnWidth		列宽	
	 * @param fieldName			对应字段名，即资源配置后缀的数组
	 * @param dataType			对应字段数据类型
	 * @param list				数组存储数据
	 * @throws Exception
	 */
	public WritableWorkbook  toExcel(WritableWorkbook workbook,String title,String prefix,int[] columnWidth,String[] fieldName) throws Exception{
		ExcelFormat excelFormat = new ExcelFormat();
		WritableSheet wsheet = workbook.createSheet("one page", 0);

		excelFormat.cellTitleFill(wsheet, 2,title, prefix, fieldName,columnWidth);
		return workbook;
	}
	/**
	 * 分页标题
	 * @param request
	 * @param workbook
	 * @param row
	 * @param prefix
	 * @param fieldName
	 * @throws Exception
	 */
	public void writePageTitle(WritableWorkbook workbook,int row,String prefix,String[] fieldName)throws Exception{
		ExcelFormat excelFormat = new ExcelFormat();
		excelFormat.cellTitleFill(workbook.getSheet(0), row, prefix, fieldName);
	}
	/**
	 * 填充List数据(PO绑定)
	 * @param request
	 * @param workbook		excel workbook
	 * @param dataType		数据类型
	 * @param list			数据
	 * @param methodName	方法名称
	 * @throws Exception
	 */
	public void writePoList(WritableWorkbook workbook,String[] dataType,List list,String methodName)throws Exception{
		int row = workbook.getSheet(0).getRows();
		this.writePoList(workbook, row, dataType, list, methodName);
	}
	
	/**
	 * 填充List数据(PO绑定，指定行号)
	 * @param request
	 * @param workbook		excel workbook
	 * @param row			填充数据的起始行号
	 * @param dataType		数据类型
	 * @param list			数据
	 * @param methodName	方法名称
	 * @throws Exception
	 */
	public void writePoList(WritableWorkbook workbook,int row,String[] dataType,List list,String methodName)throws Exception{
		ExcelFormat excelFormat = new ExcelFormat();
		for (int i = row; i < list.size() + row; i++) {
			Object obj = (Object)list.get(i-row);
			String[] arg = new String[0];
			String[] toRptString = (String[])this.invokeMethod(obj, methodName, arg);
			excelFormat.cellContentFill(workbook.getSheet(0), i, toRptString, dataType);
		}
	}
	
	/**
	 * 填充List数据（不绑定PO）
	 * @param request
	 * @param workbook
	 * @param fieldName		字段名
	 * @param dataType		数据类型
	 * @param list			数组
	 * @throws Exception
	 */
	public void writePoList(WritableWorkbook workbook,int sheetIdx, String[] fieldName,String[] dataType,List list,String[] resTranslate) throws Exception{
		ExcelFormat excelFormat = new ExcelFormat();
		int row = workbook.getSheet(sheetIdx).getRows();
		for (int i = row; i < list.size() + row; i++) {
			Pojo po = (Pojo)list.get(i-row);
			excelFormat.cellContentFill(workbook.getSheet(sheetIdx), i, toRptString(po,fieldName,dataType,resTranslate), dataType);
		}
	}
	/**
	 * 填充数据对象
	 * @param request
	 * @param workbook
	 * @param fieldContent	数据内容
	 * @param dataType		数据字段类型
	 * @throws Exception
	 */
	public void writeContent(WritableWorkbook workbook,String[] fieldContent,String[] dataType)throws Exception{
		ExcelFormat excelFormat = new ExcelFormat();
		int row = workbook.getSheet(0).getRows();
		excelFormat.cellContentFill(workbook.getSheet(0), row, fieldContent,dataType);
	}
	/**
	 * 填充数据对象(指定行号)
	 * @param request
	 * @param workbook
	 * @param row			起始行号
	 * @param fieldContent  数据内容
	 * @param dataType		数据字段类型
	 * @throws Exception
	 */
	public void writeContent(WritableWorkbook workbook,int row,String[] fieldContent,String[] dataType)throws Exception{
		ExcelFormat excelFormat = new ExcelFormat();
		excelFormat.cellContentFill(workbook.getSheet(0), row, fieldContent,dataType);
	}
	/**
	 * 取得PO对象值存入数组
	 * @param po			po对象
	 * @param fieldName		指定的po字段
	 * @param dataType		数据类型及呈现格式
	 * @return
	 */
	public String[] toRptString(Pojo po, String[] fieldName,String[] dataType,String[] resTranslate){
		String[] fieldContent =  new String[fieldName.length];
		for( int i =0;i< fieldName.length; i++){
			Object obj = null;
			try{
				obj = PropertyUtils.getProperty(po,fieldName[i]);
			}catch(Exception e){
			}
			if (obj == null ) 
				fieldContent[i] = "";
			else{
				if(resTranslate[i] != null){
					obj = ResConfig.getProperty(resTranslate[i]+obj);
				}
				fieldContent[i] = String.format(dataType[i], obj);
			}
		}
		return fieldContent;
	}
	
	/**
	 * 类方法反射，执行指定方法
	 * @param owner			对象
	 * @param methodName	方法名称
	 * @param args			参数数组
	 * @return
	 * @throws Exception
	 */
	public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
	      Class ownerClass = owner.getClass();
	      Class[] argsClass = new Class[args.length];
	  
	      for (int i = 0, j = args.length; i < j; i++) {
	          argsClass[i] = args[i].getClass();
	      }
	     Method method = ownerClass.getMethod(methodName, argsClass);
	     return method.invoke(owner, args);
	}
}
